using System.Diagnostics.CodeAnalysis;
using CultivationSkills.Utils;
using ThunderRoad;
using UnityEngine;

namespace CultivationSkills.Skills;

[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
[SuppressMessage("ReSharper", "FieldCanBeMadeReadOnly.Global")]
[SuppressMessage("ReSharper", "ConvertToConstant.Global")]
public class SkillSwordBlink : IMySkill
{
    public static ModOptionBool[] BoolDefaultValues =
    [
        new("Open", true),
        new("Close", false)
    ];

    [ModOption(name = "Sword Blink Enabled", order = 1, category = "Sword Blink")]
    public static bool Enabled = true;

    public void Init()
    {
        Debug.Log($"[{nameof(SkillSwordBlink)}] Initializing.");
        // EventManager.OnItemGrab += (handle, hand) => { handle.item.OnFlyStartEvent += OnThrow; };
        EventManager.OnItemRelease -= OnItemRelease;
        PlayerControl.local.OnButtonPressEvent -= OnButtonPressEvent;
        EventManager.OnItemRelease += OnItemRelease;
        PlayerControl.local.OnButtonPressEvent += OnButtonPressEvent;
    }

    public void Clear()
    {
        EventManager.OnItemRelease -= OnItemRelease;
        PlayerControl.local.OnButtonPressEvent -= OnButtonPressEvent;
    }
    
    private static float _throwTime = 0;
    private static Item _throwingItem;

    public static void OnThrow(Item item)
    {
        if (!Enabled)
        {
            return;
        }

        _throwTime = Time.time;
        _throwingItem = item;
        Debug.Log("_throwTime="+_throwTime);
        Debug.Log("_throwingItem="+_throwingItem);
    }

    public static void Billing(PlayerControl.Hand hand)
    {
        Debug.Log("结算1");
        if (_throwingItem == null || !Enabled || !Player.local)
        {
            return;
        }
        if (PlayerControl.GetHand(hand.side).usePressed)
        {
            MyAssets.PlayAudioOnPlayer(MyAudios.ChuJian);
            ForcePlayerGrab(hand);
            Debug.Log("结算2");
            return;
        }

        if (Time.time - _throwTime >= 3)
        {
            Debug.Log("超时："+ (Time.time - _throwTime));
            return;
        }
        Debug.Log("结算3");

        var itemPosition = _throwingItem.transform.position;
        Player.local.gameObject.transform.position = itemPosition;
        MyAssets.PlayAudioOnPlayer(MyAudios.Blink);

        ForcePlayerGrab(hand);

        // 失重
        MakeWeightless();
    }

    // 调用这个方法来使人物失重
    public static void MakeWeightless()
    {
        var rb = Player.local.GetComponent<Rigidbody>();
        if (!rb) return;
        // 设置重力缩放为0，使物体失重
        if (rb.useGravity)
        {
            rb.useGravity = false;
        }
        else
        {
            Player.local.CancelInvoke(nameof(RestoreGravity));
        }

        // 1秒后恢复重量
        Player.local.Invoke(nameof(RestoreGravity), 1.5f);
    }

    // 这个方法用于恢复物体的重力
    private static void RestoreGravity()
    {
        var rb = Player.local.GetComponent<Rigidbody>();
        if (!rb) return;
        rb.useGravity = true;
    }

    public static void ForcePlayerGrab(PlayerControl.Hand hand)
    {
        if (!_throwingItem)
        {
            return;
        }

        if (!(bool)(UnityEngine.Object)Player.local)
            return;
        var playerHand = hand.side == Side.Right
            ? Player.local.handRight
            : Player.local.handLeft;
        if (!(bool)(UnityEngine.Object)playerHand || !(bool)(UnityEngine.Object)playerHand.ragdollHand)
            return;
        if (playerHand.ragdollHand.grabbedHandle)
        {
            playerHand.ragdollHand.UnGrab(false);
        }

        // 先移回来
        _throwingItem.transform.position =
            Player.local.head.transform.position + Player.local.head.transform.forward;
        playerHand.ragdollHand.Grab(_throwingItem.GetMainHandle(hand.side));
    }

    private static void OnButtonPressEvent(PlayerControl.Hand hand, PlayerControl.Hand.Button button, bool pressed)
    {
        Debug.Log("button:"+ button+" pressed: "+pressed);
        if (button != PlayerControl.Hand.Button.Grip || !pressed)
        {
            return;
        }

        Billing(hand);
    }

    private static void OnItemRelease(Handle handle, RagdollHand hand, bool throwing)
    {
        Debug.Log("OnItemRelease1:"+throwing);
        if (throwing)
        {
            OnThrow(handle.item);
        }
    }
}